<template>
  <div>
    <van-button class="m-add" type="primary" block @click="show = true">添加成本</van-button>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多数据了"
      @load="onLoad"
    >
      <div
        v-for="item in list"
        :key="item.id"
        class="m-cost"
      >
        <van-row>
          <van-col span="10"> {{ item.createTime }}</van-col>
          <van-col span="3"> {{ item.sort | sortFlt }}</van-col>
          <van-col span="6" style="color: green; font-weight: bold">{{ item.money }}元</van-col>
          <van-col v-if="item.status === 0" span="5" style="color:rgba(236, 13, 13, 0.705)">待财务确认</van-col>
          <van-col v-if="item.status === 1" span="5" style="color:#cfab1ce1">待老板确认</van-col>
          <van-col v-if="item.status === 2" span="5" style="color:green">老板已确认</van-col>
        </van-row>
        <div style="color: grey;">{{ item.type }}</div>
      </div>
    </van-list>
    <van-popup
      v-model="show"
      closeable
      :style="{ height: '70%', width: '100%' }"
    >
      <van-form label-width="56px" style="margin-top: 50px;" @submit="onSubmit">
        <van-picker
          :show-toolbar="false"
          :columns="columns"
          @change="onChange"
        />
        <van-field
          v-model="spend.money"
          type="number"
          label="成本・元"
          :rules="[{ required: true, message: '必填项' }]"
        />
        <van-field
          v-model="spend.type"
          label="成本说明"
          placeholder="选填"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit">提交</van-button>
        </div>
      </van-form>
    </van-popup>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import List from 'vant/lib/list'
import Row from 'vant/lib/row'
import Col from 'vant/lib/col'
import 'vant/lib/list/style'
import 'vant/lib/row/style'
import 'vant/lib/col/style'
import Button from 'vant/lib/button'
import 'vant/lib/button/style'
import { getSpendBySid, addSpend } from '@/api/ym'
import Form from 'vant/lib/form'
import Field from 'vant/lib/field'
import 'vant/lib/form/style'
import 'vant/lib/field/style'
import Popup from 'vant/lib/popup'
import 'vant/lib/popup/style'
import Notify from 'vant/lib/notify'
import 'vant/lib/notify/style'
import Picker from 'vant/lib/picker'
import 'vant/lib/picker/style'

export default {
  components: {
    [Picker.name]: Picker,
    [Notify.name]: Notify,
    [Form.name]: Form,
    [Field.name]: Field,
    [Popup.name]: Popup,
    [Button.name]: Button,
    [List.name]: List,
    [Row.name]: Row,
    [Col.name]: Col
  },
  filters: {
    sortFlt (sort) {
      switch (sort) {
        case 0:
          return '房租'
        case 1:
          return '物业'
        case 2:
          return '水电'
        case 3:
          return '宽带'
        case 4:
          return '管家'
        case 5:
          return '操作'
        case 6:
          return '其他'
        case 7:
          return '耗材'
        case 8:
          return '三方'
        case 9:
          return '流量'
        case 10:
          return '客情'
        case 11:
          return '运营'
      }
    }
  },
  props: {
    sname: {
      type: String,
      default () {
        return ''
      }
    }
  },
  data () {
    return {
      spend: {
        sort: 0,
        type: '',
        money: 0,
        status: 0
      },
      columns: ['房租', '物业', '水电', '宽带', '管家团队', '操作团队', '其他团队', '耗材药品', '三方合作', '流量费用', '客情支出', '运营杂费'],
      show: false,
      list: [],
      loading: false,
      finished: false
    }
  },
  computed: {
    ...mapGetters(['openId'])
  },
  methods: {
    onChange(picker, value, index) {
      this.spend.sort = index
    },
    onLoad () {
      this.spend.sid = this.openId
      this.spend.sname = this.sname
      this.loading = false
      getSpendBySid(this.openId, -1).then(res => {
        this.list = res.data.data
        this.finished = true
      })
    },
    onSubmit () {
      addSpend(this.spend).then(res => {
        this.list.unshift(res.data.data)
        this.spend.type = ''
        this.spend.money = 0
        this.show = false
        Notify({ type: 'success', message: '提交成功' })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.m-cost{
  background-color: #fff;
  // color:#C31171;
  font-size: 26px;
  line-height: 60px;
  text-align: center;
  box-shadow: 0px 0px 5px rgb(201, 201, 201);
  margin-bottom: 3px;
}
.m-add {
  margin-bottom: 3px;
}
</style>
